import React from 'react';
import { Card, Form, Input, Button, message } from 'antd';
import { UserOutlined, LockOutlined, CloudServerOutlined } from '@ant-design/icons';
import { useNavigate } from 'react-router-dom';
import useStore from '../store';
import { loginApi } from '../services/api';

const Login = () => {
  const navigate = useNavigate();
  const setToken = useStore((state) => state.setToken);
  const setUser = useStore((state) => state.setUser);
  const [loading, setLoading] = React.useState(false);

  const onFinish = async (values) => {
    try {
      setLoading(true);
      const response = await loginApi(values);
      console.log('Login response:', response);

      if (response.token && response.user) {
        setToken(response.token);
        setUser(response.user);
        message.success('登录成功');
        navigate('/dashboard');
      } else {
        message.error('登录失败：返回数据格式错误');
      }
    } catch (error) {
      console.error('Login failed:', error);
      message.error(error.message || '登录失败，请重试');
    } finally {
      setLoading(false);
    }
  };

  return (
    <div style={{ 
      height: '100vh', 
      display: 'flex', 
      justifyContent: 'center', 
      alignItems: 'center',
      background: '#f0f2f5' 
    }}>
      <Card style={{ width: 400 }}>
        <div style={{ textAlign: 'center', marginBottom: 30 }}>
          <CloudServerOutlined style={{ 
            fontSize: 48, 
            color: '#1890ff',
            marginBottom: 16,
          }} />
          <h2 style={{ 
            margin: 0,
            fontSize: 24,
            fontWeight: 600,
          }}>
            React Admin
          </h2>
          <p style={{ 
            margin: '8px 0 0',
            color: 'rgba(0,0,0,0.45)',
            fontSize: 14,
          }}>
            企业级中后台管理系统
          </p>
        </div>
        <Form
          name="login"
          onFinish={onFinish}
          initialValues={{
            username: 'admin',
            password: '123456'
          }}
        >
          <Form.Item
            name="username"
            rules={[{ required: true, message: '请输入用户名' }]}
          >
            <Input 
              prefix={<UserOutlined />} 
              placeholder="用户名 (admin)" 
              size="large"
            />
          </Form.Item>
          <Form.Item
            name="password"
            rules={[{ required: true, message: '请输入密码' }]}
          >
            <Input.Password 
              prefix={<LockOutlined />} 
              placeholder="密码 (123456)" 
              size="large"
            />
          </Form.Item>
          <Form.Item>
            <Button 
              type="primary" 
              htmlType="submit" 
              block 
              size="large"
              loading={loading}
            >
              登录
            </Button>
          </Form.Item>
          <div style={{ textAlign: 'center', color: '#999' }}>
            默认账号：admin / 123456
          </div>
        </Form>
      </Card>
    </div>
  );
};

export default Login; 